import 'package:enterprise_v6_app/app/components/custom_modele/custom_card.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_dropdown_selection.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/utils/file_util.dart';
import 'package:enterprise_v6_app/app/data/entities/application/major_hazard/hazard_archive_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/archives/archives_controller.dart';

class HazardArchivesPage extends StatelessWidget {
  HazardArchivesPage({super.key});

  final controller = Get.put(HazardArchivesController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          body: Container(
            padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 14.w),
            alignment: Alignment.topCenter,
            color: const Color(0xffF5F5F6),
            child: Column(
              children: [
                CustomDropdownSelection(
                  text: controller.select?.name,
                  hintText: '选择类别',
                  onCallBack: controller.showSelect,
                ),
                AppGaps.vGap10,
                Expanded(
                  child: CustomEasyRefreshList<HazardArchiveEntity>(
                    key: ValueKey(controller.select),
                    dataLoad: controller.dataLoad,
                    itemBuilder: _buildArchieveCard,
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  Widget _buildArchieveCard(HazardArchiveEntity data, int idx) {
    return CustomCard(
      onTap: () => FileUtils.openFile(data.fileUrl ?? '', title: data.fileName),
      padding: EdgeInsets.zero,
      titleWidget: Padding(
        padding: EdgeInsets.all(14.w),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                SvgPicture.asset(AppImages.icon_title),
                AppGaps.hGap10,
                Text(
                  data.fileName ?? '未知文件名',
                  style: TextStyle(fontSize: 15.w, fontWeight: FontWeight.w500),
                ),
              ],
            ),
            SvgPicture.asset(AppImages.icon_job_right),
          ],
        ),
      ),
      divider: CustomCard.builtinDivider,
      content: Padding(
        padding: EdgeInsets.all(14.w),
        child: Column(
          children: [
            _buildDetail('创建人：', data.createBy ?? '未知'),
            AppGaps.vGap10,
            _buildDetail(
              '联系方式：',
              data.createMobile ?? '未知',
            ),
            AppGaps.vGap10,
            _buildDetail('创建时间：', data.createDate ?? '未知')
          ],
        ),
      ),
    );
  }

  Widget _buildDetail(String title, String val) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          title,
          style: TextStyle(fontSize: 13.w, color: AppColors.font_color_gray),
          textAlign: TextAlign.start,
        ),
        Text(
          val,
          style: TextStyle(fontSize: 12.w, color: AppColors.font_color_black),
          textAlign: TextAlign.end,
        ),
      ],
    );
  }
}
